﻿function Widget_StickyNotes(obj)
{
   this.initializeBase(obj);
}
Widget_StickyNotes.prototype = new WidgetInstance;

Widget_StickyNotes.prototype.Load = function()
{
   //WriteHistory("Widget_StickyNotes.prototype.Load");

   this.Widget.InstanceLoad();
   //runs any script element inside the div
   this.TextArea = $get("txt_StickyNotes" + this.id);

   if (this.Widget.obj.State && this.Widget.obj.State.length > 0)
   {
      this.TextArea.value = this.Widget.obj.State;
      if (this.Widget.obj.Height > 0)
		this.ResizeHeightComplete(this.Widget.obj.Height + "px");
	  else	  
	  {
		this.SetRows();
	  }
   }
   //execJS(this.WidgetDiv);   
   this.Widget.InstanceLoadComplete();
}

Widget_StickyNotes.prototype.Save = function()
{
   if (myTabs.hasRequiredTabPermission(this.Tab, TabAccessLevel.Widget_Edit))
   {
      //Save only if changed
      if (this.TextArea.value != this.Widget.obj.State)
         JSON.Widget_UpdateProperty(this.Widget.Tab.id, this.id, "state", this.TextArea.value);
   }
}

Widget_StickyNotes.prototype.RefreshContent = function()
{
   JSON.Widget_Refresh(this.Widget.Tab.id, this.id, Function.createDelegate(this, this.RefreshComplete));
}

Widget_StickyNotes.prototype.RefreshComplete = function(obj)
{
   //returns a JSON object
   this.Widget.obj = obj;
   this.TextArea.value = this.Widget.obj.State;
   this.ResizeHeightComplete(this.Widget.obj.Height + "px");
}

Widget_StickyNotes.prototype.SetRows = function()
{
   var textarea = this.TextArea;
   var cols = textarea.cols;
   var str = textarea.value;
   str = str.replace(/\r\n?/, "\n");
   var lines = 2;
   var chars = 0;
   for (i = 0; i < str.length; i++)
   {
      var c = str.charAt(i);
      chars++;
      if (c == "\n" || chars == cols)
      {
         lines ++;
      chars = 0;
      }
   }
   textarea.setAttribute("rows", lines);
   if (parseInt(textarea.style.height) < lines*15)
      textarea.style.height = lines*15 + "px";
}

Widget_StickyNotes.prototype.ResizeHeightComplete = function(theheight)
{
   this.TextArea.style.height = theheight;
}
